<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CampaignsController
 *
 * @author Admin
 */
class Back_CampaignsController extends Zend_Controller_Action
{
    public function init() {
        $this->_helper->layout->setLayout('admin');
    }
    
    public function indexAction() {
        $campaigns = new Back_Model_Campaigns();        
        $allCampaigns = $campaigns->getAllCampaigns();
        foreach ($allCampaigns as $key => $campaign) {                        
            $startBudget = $campaign['start'];
            $currentBudget = $campaign['current'];            
            $costs = $startBudget - $currentBudget;
            $allCampaigns[$key]['costs'] = $costs;
        }
        $this->view->allCampaigns = $allCampaigns;                
        
        //типы рекламы
        $banner = 1; $adver = 2; $blog = 3; $social = 4; $video = 5;        
        //статусы
        $active = 1; $notConfirmed = 2; $blocked = 3;                
        /**
         * Campaigns
         */        
        $countCampaigns = $campaigns->getCountAllCampaigns();
        $this->view->countCampaigns = $countCampaigns;                        
        $countActiveCampaigns = $campaigns->getCountStatusCampaigns($active);
        $this->view->countActiveCampaigns = $countActiveCampaigns;        
        $countNotConfirmedCampaigns = $campaigns->getCountStatusCampaigns($notConfirmed);
        $this->view->countNotConfirmedCampaigns = $countNotConfirmedCampaigns;
        $countBlockedCampaigns = $campaigns->getCountStatusCampaigns($blocked);
        $this->view->countBlockedCampaigns = $countBlockedCampaigns;
        
        /**
         * Advertisements
         */
        $countAdvertisements = $campaigns->getCountAllAdvertisements();
        $this->view->countAdvertisements = $countAdvertisements;
        $countBannerAdvertisements = $campaigns->getCountStatusAdvertisements($banner);
        $this->view->countBannerAdvertisements = $countBannerAdvertisements;
        $countAdverAdvertisements = $campaigns->getCountStatusAdvertisements($adver);
        $countVideoAdvertisements = $campaigns->getCountStatusAdvertisements($video);
        $countContextAdvertisements = $countAdverAdvertisements['count'] + $countVideoAdvertisements['count'];
        $this->view->countContextAdvertisements = $countContextAdvertisements;
        $countBlogAdvertisements = $campaigns->getCountStatusAdvertisements($blog);
        $this->view->countBlogAdvertisements = $countBlogAdvertisements;
        $countSocialAdvertisements = $campaigns->getCountStatusAdvertisements($social);
        $this->view->countSocialAdvertisements = $countSocialAdvertisements;
        
        /*
         * Payments advertisers
         */
        $statistic = new Back_Model_Statistic();
        $allPayments = $statistic->getAllAdvPayments();
        $this->view->allPayments = $allPayments;
        $lastMonthPayments = $statistic->getLastMonthAdvPayments();
        $this->view->lastMonthPayments = $lastMonthPayments;
        $reservedForCampaigns = $statistic->getCurrentBalanceActiveCampaigns();
        $this->view->reservedForCampaigns = $reservedForCampaigns;
        $todayPayments = $statistic->getTodayAdvPayments();
        $this->view->todayPayments = $todayPayments;
    }
    
    public function deletecampaignAction() {
        $idCampaign = $this->_getParam('id');
        $campaigns = new Back_Model_Campaigns();
        $campaigns->deleteSelectCampaign($idCampaign);
        $this->_helper->redirector('index', 'campaigns');
    }
    
    public function changestatusAction() {
        $idCampaign = $this->_getParam('id');
        $idStatus = $this->_getParam('status');
        $data = array(
            'id_status' => $idStatus
        );
        $campaigns = new Back_Model_Campaigns();
        $campaigns->changeCurrentStatus($idCampaign, $data);
        $this->_helper->redirector('index', 'campaigns');
    } 
}

?>
